package com.fairytask.ui.item.activeItem
{
	import com.fairytask.data.BaseData;
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.Scroll.ScrollPanel;
	import com.fairytask.engine.components.Scroll.ScrollSkin;
	import com.fairytask.engine.components.table.TableEvent;
	import com.fairytask.engine.components.table.TableItem;
	import com.fairytask.engine.components.table.TableRow;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.utils.draw.DrawAPI;
	import com.fairytask.engine.utils.draw.DrawStyle;
	import com.fairytask.managers.ResManager;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * 活跃度表格 
	 * @author xujh
	 * 
	 */
	public class ActiveTable extends BaseComponent
	{
		//属性
		private var _titleSkin		:Sprite;
		private var _itemSkinArr	:Array;
		private var _maxWidth		:int	=	0;
		//显示
		private var _scrollPanel	:ScrollPanel;
		private var _mainSpace		:Sprite	=	new Sprite(); //主容器
		private var _titleSpace		:Sprite	=	new Sprite();
		private var _bodySpace		:Sprite	=	new Sprite();
		private var _selectShap		:Sprite	=	new Sprite();	//选中
		private var _unCompSpace	:Sprite	=	new Sprite();	//未完成
		private var _compSpace		:Sprite	=	new Sprite();	//已完成
		private var _unCompTit		:Label	=	new Label("FFCB4A",14);
		private var _compTit		:Label	=	new Label("FFCB4A",14);
		//选中行
		private var _selectRow		:ActiveTableRow	=	null;
		//数据
		private var _titleArr		:Array	=	[];
		private var _titleStrArr	:Array	=	[];	//表头信息
		private var _sizeData		:Array	=	[];	//格子尺寸
		private var _unCompArr		:Array	=	[];
		private var _compArr		:Array	=	[];
		
		private var _cols			:int	=	0;	//列数
		public static var colSplit:int	=	1;
		public static var rowSplit:int	=	24;	//这值是内容距离顶部标题的距离(2级标题高度)
		
		//默认行高19
		private var _lineHeight	:int	=	22;
		//出去行高19的行间距，默认0
		private var _rowSpace		:int	=	0;
		//未完成及已完成之间的间隔
		private var _tableBlank		:int	=	10;
		
		/**
		 * 表格初始化 
		 * @param _w 宽
		 * @param _h 高
		 * @param _titleArr 表格头，string数组
		 * @param _skArr 皮肤 string 数组
		 * @param _sizeArr 单格尺寸数组
		 */		
		public function ActiveTable(_w:int,_h:int,_titleStrArr:Array,_skArr:Array=null,_sizeArr:Array=null)
		{
			if(_sizeArr!=null)
			{
				_sizeData	=	_sizeArr;	//自定义大小
			}
			
			_selectShap.alpha	=	0;
			if(_skArr!=null)
			{
				_titleSkin		=	ResManager.newResClass(_skArr.shift()) as Sprite;		//提取素材资源
				while(_skArr.length>0)
				{
					_itemSkinArr.push(ResManager.newResClass(_skArr.shift()));
				}
			}
			_titleSpace.y	=	5;			//列标题	空间
			addChild(_titleSpace);
			//滚动条 区域
			_scrollPanel	=	new ScrollPanel(new Rectangle(0,0,_w,_h-30),_bodySpace,new ScrollSkin("vScrol_1","vScrol_5"));
			_scrollPanel.y	=	25;
			addChild(_scrollPanel);
			_titleStrArr	=	_titleStrArr;
			initTitle(_titleStrArr);
			initSecondTitle();
			_bodySpace.addChild(_unCompSpace);
			_bodySpace.addChild(_compSpace);
		}
		
		/**
		 * 初始化表头 
		 * @param arr
		 * 
		 */
		private function initTitle(arr:Array):void
		{
			//清空容器
			while(_titleSpace.numChildren>0)
			{
				_titleSpace.removeChildAt(0);
			}
			
			_cols	=	_titleArr.length;		//列数
			var tmpRow:TableRow	=	new TableRow(true);		
			for(var idx:int=0;idx<arr.length;idx++)
			{
				var tmpItem:TableItem	=	new TableItem(arr[idx],true,_sizeData[idx]);
				tmpItem.setBorder([Filter.EXP_FILTER]);
				tmpItem.txtColor	=	0xFFFFFF;
				tmpRow.addItem(tmpItem);
				_titleArr.push(tmpItem);
			}
			_titleSpace.addChild(tmpRow);
		}
		
		/**
		 * 初始化二级表头
		 * 
		 */
		private function initSecondTitle():void
		{
			//未完成项目
			_unCompTit.htmlStr	=	StaticIni.getSwfLangStr("1000000650");
			_unCompSpace.addChild(_unCompTit);
			//已完成项目
			_compTit.htmlStr	=	StaticIni.getSwfLangStr("1000000652");
			_compSpace.addChild(_compTit);
			_compSpace.y	=	30;
		}
		
		/**
		 * 添加行 
		 * @param labelArr
		 * @param pNote 0未完成1已完成
		 * @param rowData
		 * 
		 */
		public function addRow(labelArr:Array,pNote:int=0,rowData:BaseData=null,obj:BaseComponent=null,onClick:Function=null):void
		{
			var tmpRow:ActiveTableRow	=	new ActiveTableRow();		//一条数据
			for(var i:int=0;i<labelArr.length;i++)
			{
				var tmpItem:TableItem	=	new TableItem(labelArr[i],false,_sizeData[i]);//TableItem就是一个Label		_sizeData[idx]列之间间隙
				tmpItem.pos	=	i;
				tmpRow.addTableItem(tmpItem);		//添加完整一条数据
			}
			//添加其他对象
			if(null != obj)
			{
				tmpRow.addTableItem(obj);
				if(null != onClick)
				{
					obj.addEventListener(MouseEvent.CLICK,onClick);
				}
			}
			tmpRow.addEventListener(TableEvent.ROW_SELECT,onItemClick);//事件
			tmpRow.addEventListener(TableEvent.ITEM_LINK,onItemClick);
			tmpRow.addEventListener(TableEvent.ROW_OVER,onOver);
			tmpRow.addEventListener(TableEvent.ROW_OUT,onOut);
			tmpRow.data		=	rowData;
			tmpRow.pNote	=	pNote;
			if(pNote == 0)
			{
				//未完成
				addToNote(tmpRow,_unCompArr,_unCompSpace);
			}
			else
			{
				addToNote(tmpRow,_compArr,_compSpace);
			}
			if(_maxWidth<tmpRow.itemPox)
			{
				_maxWidth	=	tmpRow.itemPox;
				if(_selectShap.numChildren==0)
				{
					//画选中效果
					DrawAPI.drawRect(_selectShap.graphics,DrawStyle.getRectStyle(0,0,_maxWidth,20),DrawStyle.getFillStyle(uint(0xC3B61A),.7));
				}
			}
			updataBodySpace();
		}
		
		/**
		 * 添加到节点
		 * @param pNote 0未完成1已完成
		 * 
		 */
		private function addToNote(tmpRow:ActiveTableRow,dataArr:Array,sp:Sprite):void
		{
			tmpRow.pos	=	dataArr.length;
			dataArr.push(tmpRow);
			tmpRow.y	=	sp.height + rowSplit;
			sp.addChild(tmpRow);
		}
		
		/**
		 * 选中一行 
		 * @param evt
		 */
		private function onItemClick(evt:TableEvent):void
		{
			_bodySpace.addChildAt(_selectShap,0);
			_selectShap.alpha	=	1;
			_selectShap.y	=	evt.currentTarget.y	+	evt.currentTarget.parent.y;//选中框移动
			_selectRow	=	evt.currentTarget as ActiveTableRow;
			dispatchEvent(evt);
		}
		
		/**
		 * tip 
		 * @param evt
		 * 
		 */
		private function onOver(evt:TableEvent):void
		{
			TipsManage.showTips(evt.currentTarget.data.GetProperties("Describe"));
		}
		
		private function onOut(evt:TableEvent):void
		{
			ToolTips.hide();
		}
		
		/**
		 * 修改某一行某一列数据 
		 * @param row
		 * @param col
		 * @param val
		 * 
		 */
		public function editRowItem(pNote:int,row:int,col:int,val:String):void
		{
			
		}
		
		/**
		 *	重绘Table Body容器 
		 */		
		public function updataBodySpace():void
		{
			updataRow(_unCompArr,_unCompSpace);
			updataRow(_compArr,_compSpace);
			//检查显示
			checkShow(_unCompArr,_unCompSpace);
			checkShow(_compArr,_compSpace);
			if(_unCompSpace.visible)
			{
				if(_compSpace.visible)
				{
					_compSpace.y	=	(_lineHeight + _rowSpace) * _unCompSpace.numChildren + rowSplit;
				}
			}
			else
			{
				_compSpace.y	=	0;
			}
			_scrollPanel.update();			//滚动条更新
		}
		
		/**
		 * 分块重绘
		 * 
		 */
		private function updataRow(dataArr:Array,sp:Sprite):void
		{
			//先清除，标题保留
			while(sp.numChildren>1)
			{
				sp.removeChildAt(1);
			}
			var len:int	=	dataArr.length;
			if(_maxWidth < sp.width)
			{
				_maxWidth	=	sp.width;
			}
			for(var i:int=0;i<len;i++)
			{
				var tmpRow:ActiveTableRow	=	dataArr[i];
				tmpRow.pos	=	i;
				tmpRow.y	=	(_lineHeight + _rowSpace) * i + rowSplit;
				//tmpRow.y	=	_bodySpace.height + Table.rowSplit;
				sp.addChild(tmpRow);				
			}
		}
		
		/**
		 * 检查显示，子对象为0不显示 
		 * @param arr	对象数组
		 * @param sp	显示对象
		 * 
		 */
		private function checkShow(arr:Array,sp:Sprite):void
		{
			if(arr.length > 0)
			{
				sp.visible	=	true;
			}
			else
			{
				sp.visible	=	false;
			}
		}
		
		/**
		 * 清除表格 
		 * 
		 */
		public function clear():void
		{
			_unCompArr	=	[];
			_compArr	=	[];
		}
	}
}